﻿using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp1.Commons;
using WpfApp1.Model;

namespace WpfApp1.ViewModel
{
    public class FirstPageViewModel:NotifyBase
    {
        private int _instrumentValue=0;

        public int InstrumentValue
        {
            get { return _instrumentValue; }
            set { _instrumentValue = value; this.DoNotify(); }
        }

        public ObservableCollection<CourseSeriesModel> CourseSeriesList { get; set; } 
            = new ObservableCollection<CourseSeriesModel>();
        public FirstPageViewModel()
        {
            this.RefreshInstrumentValue();
            this.InitCourseSeries();
        }
        private void InitCourseSeries()
        {
            CourseSeriesList.Add(new CourseSeriesModel
            {
                CourseName = "JavaEE实战基础",
                SeriesCollection = new LiveCharts.SeriesCollection 
                { 
                    new PieSeries
                    {
                        Title="网工" ,Values=new ChartValues<ObservableValue>
                        { 
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="计科" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="物网" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="软工" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="数据" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    }
                },
                SeriesList=new ObservableCollection<SeriesModel> 
                {
                    new SeriesModel{SeriesName="网工",CurrentValue=161,IsGrowing=false,ChangeRate=-75},
                    new SeriesModel{SeriesName="计科",CurrentValue=161,IsGrowing=false,ChangeRate=-75},
                    new SeriesModel{SeriesName="物网",CurrentValue=161,IsGrowing=true,ChangeRate=75},
                    new SeriesModel{SeriesName="软工",CurrentValue=161,IsGrowing=false,ChangeRate=-75},
                    new SeriesModel{SeriesName="数据",CurrentValue=161,IsGrowing=true,ChangeRate=135}
                }
            });
            CourseSeriesList.Add(new CourseSeriesModel
            {
                CourseName = "Oracle数据库",
                SeriesCollection = new LiveCharts.SeriesCollection
                {
                    new PieSeries
                    {
                        Title="网工" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="计科" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="物网" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="软工" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="数据" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    }
                },
                SeriesList = new ObservableCollection<SeriesModel>
                {
                    new SeriesModel{SeriesName="网工",CurrentValue=161,IsGrowing=false,ChangeRate=-75},
                    new SeriesModel{SeriesName="计科",CurrentValue=161,IsGrowing=false,ChangeRate=-75},
                    new SeriesModel{SeriesName="物网",CurrentValue=161,IsGrowing=false,ChangeRate=-75},
                    new SeriesModel{SeriesName="软工",CurrentValue=161,IsGrowing=false,ChangeRate=-75},
                    new SeriesModel{SeriesName="数据",CurrentValue=161,IsGrowing=false,ChangeRate=-75}
                }
            });
            CourseSeriesList.Add(new CourseSeriesModel
            {
                CourseName = "Vue框架精通",
                SeriesCollection = new LiveCharts.SeriesCollection
                {
                    new PieSeries
                    {
                        Title="网工" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="计科" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="物网" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="软工" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="数据" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    }
                },
                SeriesList = new ObservableCollection<SeriesModel>
                {
                    new SeriesModel{SeriesName="网工",CurrentValue=161,IsGrowing=false,ChangeRate=-75},
                    new SeriesModel{SeriesName="计科",CurrentValue=161,IsGrowing=true,ChangeRate=25},
                    new SeriesModel{SeriesName="物网",CurrentValue=161,IsGrowing=false,ChangeRate=-75},
                    new SeriesModel{SeriesName="软工",CurrentValue=161,IsGrowing=false,ChangeRate=-75},
                    new SeriesModel{SeriesName="数据",CurrentValue=161,IsGrowing=true,ChangeRate=98}
                }
            });
            CourseSeriesList.Add(new CourseSeriesModel
            {
                CourseName = "Vue框架精通",
                SeriesCollection = new LiveCharts.SeriesCollection
                {
                    new PieSeries
                    {
                        Title="网工" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="计科" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="物网" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="软工" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    },
                    new PieSeries
                    {
                        Title="数据" ,Values=new ChartValues<ObservableValue>
                        {
                            new ObservableValue(123)
                        },DataLabels=false
                    }
                },
                SeriesList = new ObservableCollection<SeriesModel>
                {
                    new SeriesModel{SeriesName="网工",CurrentValue=161,IsGrowing=true,ChangeRate=-75},
                    new SeriesModel{SeriesName="计科",CurrentValue=161,IsGrowing=true,ChangeRate=75},
                    new SeriesModel{SeriesName="物网",CurrentValue=161,IsGrowing=false,ChangeRate=-125},
                    new SeriesModel{SeriesName="软工",CurrentValue=161,IsGrowing=true,ChangeRate=175},
                    new SeriesModel{SeriesName="数据",CurrentValue=161,IsGrowing=false,ChangeRate=-75}
                }
            });
        }
        Random random = new Random();
        bool taskSwith = true;
        List<Task> taskList = new List<Task>();
        private void RefreshInstrumentValue()
        {
            //产生一个线程来实时刷新数据
            var task=Task.Factory.StartNew(new Action(async() => 
            {
                while (taskSwith)
                {
                    InstrumentValue = random.Next(Math.Max(0, this.InstrumentValue - 5),
                        Math.Min(100, this.InstrumentValue + 5));
                    /*InstrumentValue = random.Next(0, 100);*/
                    await Task.Delay(1000);//停顿一秒刷新一次
                }
            
            }));
            taskList.Add(task);
        }
        public void dispose()
        {
            try
            {
                taskSwith = false;
                Task.WaitAll(this.taskList.ToArray());
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
